package com.gushenge.core.impls

import android.util.Log
import com.gushenge.atools.util.ADate
import com.gushenge.core.beans.*
import com.gushenge.core.dao.GlobalConsts
import com.gushenge.core.dao.MMKVConsts
import com.gushenge.core.dao.MMKVConsts.uid
import com.gushenge.core.md5
import com.gushenge.core.toast
import com.rxlife.coroutine.RxLifeScope
import rxhttp.awaitResult
import rxhttp.toClass
import rxhttp.wrapper.param.RxHttp

object VipImpl {
    fun getVip(listener:VipBean?.()->Unit){
        RxLifeScope().launch {
            val time = ADate.getStampAs13()
            val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
            Log.e("TAG", "aaaaa: "+time )
             RxHttp.postForm(GlobalConsts.vip)
                .add("uid", uid)
                .add("t", time)
                .add("sign", sign)
                .add("member_id", MMKVConsts.sub)
                .toClass<Code<VipBean>>()
                .awaitResult {
                    Log.e("TAG", "getVip: "+it.code )
                    if(it.code==1){
                        listener(it.data)
                    }else{
                        listener(null)
                        it.message.toast()
                    }
                }.onFailure {
                     Log.e("TAG", "aaaaa: "+it.localizedMessage )
                    listener(null)
                }
        }
    }

    //权益接口  type 权益id
    fun getQuanYi(type:Int,listener:VipQuanYiBean?.()->Unit){
        RxLifeScope().launch {
            val time = ADate.getStampAs13()
            val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
            RxHttp.postForm(GlobalConsts.quanyi)
                .add("uid", uid)
                .add("t", time)
                .add("sign", sign)
                .add("type", type)
                .add("member_id", MMKVConsts.sub)
                .toClass<Code<VipQuanYiBean>>()
                .awaitResult {
//                    Log.e("TAG", "getVip: "+it.code )
                    if(it.code==1){
                        listener(it.data)
                    }else{
                        listener(null)
                        it.message.toast()
                    }
                }.onFailure {
                    Log.e("TAG", "aaaaa: "+it.localizedMessage )
                    listener(null)
                }
        }
    }
    //福利中心接口
    fun getVipCentre(listener:CentreBean?.()->Unit){
        RxLifeScope().launch {
            val time = ADate.getStampAs13()
            val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
            RxHttp.postForm(GlobalConsts.vipCentre)
                .add("uid", uid)
                .add("t", time)
                .add("sign", sign)
                .add("member_id", MMKVConsts.sub)
                .toClass<Code<CentreBean>>()
                .awaitResult {
                    if(it.code==1){
                        listener(it.data)
                    }else{
                        listener(null)
                        it.message.toast()
                    }
                }.onFailure {
                    Log.e("TAG", "aaaaa: "+it.localizedMessage )
                    listener(null)
                }
        }
    }
    //VIP礼包
    fun getVipGift(keyword:String?,p : Int ,listener:Codes<VipCentreBottomBean>?.()->Unit){
        RxLifeScope().launch {
            val time = ADate.getStampAs13()
            val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
            RxHttp.postForm(GlobalConsts.vipCentreBottom)
                .add("uid", uid)
                .add("type", 0)
                .add("p", p)
                .add("keyword", keyword)
                .add("t", time)
                .add("sign", sign)
                .add("member_id", MMKVConsts.sub)
                .toClass<Codes<VipCentreBottomBean>>()
                .awaitResult {
                    if(it.code==1){
                        listener(it)
                    }else{
                        listener(null)
                        it.message.toast()
                    }
                }.onFailure {
                    Log.e("TAG", "aaaaa: "+it.localizedMessage )
                    listener(null)
                }
        }
    }

    //优惠券显示
    fun getVipCoupon(keyword:String?,p : Int ,listener:Codes<VipCouponBean>?.()->Unit){
        RxLifeScope().launch {
            val time = ADate.getStampAs13()
            val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
            RxHttp.postForm(GlobalConsts.vipCentreBottom)
                .add("uid", uid)
                .add("type", 3)
                .add("t", time)
                .add("p", p)
                .add("keyword", keyword)
                .add("sign", sign)
                .add("member_id", MMKVConsts.sub)
                .toClass<Codes<VipCouponBean>>()
                .awaitResult {
                    if(it.code==1){
                        listener(it)
                    }else{
                        listener(null)
                        it.message.toast()
                    }
                }.onFailure {
                    Log.e("TAG", "aaaaa: "+it.localizedMessage )
                    listener(null)
                }
        }
    }
    //VIP礼包
    fun getVipHeader(type:Int? ,listener:ArrayList<VipCentreGift>?.()->Unit){
        RxLifeScope().launch {
            val time = ADate.getStampAs13()
            val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
            RxHttp.postForm(GlobalConsts.vipCentreBottom)
                .add("uid", uid)
                .add("type", type)
                .add("t", time)
                .add("sign", sign)
                .add("member_id", MMKVConsts.sub)
                .toClass<Codes<VipCentreGift>>()
                .awaitResult {
                    if(it.code==1){
                        listener(it.data)
                    }else{
                        listener(null)
                        it.message.toast()
                    }
                }.onFailure {
                    Log.e("TAG", "aaaaa: "+it.localizedMessage )
                    listener(null)
                }
        }
    }
    // 设置头像
    fun setVipHeaderItem(type:Int? ,id:String,listener:String?.()->Unit){

        RxLifeScope().launch {
            val time = ADate.getStampAs13()
            val sign = md5("${uid}${time}fsd213ewdsadqwe2121213edsad")
            RxHttp.postForm(GlobalConsts.vipHeaderItem)
                .add("uid", uid)
                .add("type", type)
                .add("t", time)
                .add("sign", sign)
                .add("id", id)
                .add("member_id", MMKVConsts.sub)
                .toClass<Code<String>>()
                .awaitResult {
                    if(it.code==1){
                        listener(it.data.toString())
                        it.message.toast()
                    }else if(it.code==2) {
                        listener(null)
                        it.message.toast()
                    }
                    else{
                        it.message.toast()
                    }
                }.onFailure {
                    Log.e("TAG", "aaaaa: "+it.localizedMessage )
                }
        }
    }

}